/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.com.develop.store.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mx.com.develop.store.model.Cliente;

/**
 *
 * @author abel
 */
@WebServlet(name = "Login", urlPatterns = {"/login.do"},
        initParams = {
            @WebInitParam(name = "usuario", value = "abelin"),
            @WebInitParam(name = "password", value = "123")
        }
)
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //String usuario = "abelin";
        //String password = "123";
        boolean isUserValid=false;
        ServletContext cntxt = request.getServletContext();
        HttpSession session = request.getSession();
        String usuarioParametro = request.getParameter("usuario");
        String contraseniaParametro = request.getParameter("pass");

        List<Cliente> clientes = (List<Cliente>) cntxt.getAttribute("clientes");
        if (clientes != null) {
            for (Cliente c : clientes) {
                if (c.getUsuario().equals(usuarioParametro) && c.getContrasenia().equals(contraseniaParametro)) {
                    session.setAttribute("usuario", c);
                    isUserValid=true;
                    break;
                } 
            }
        } 
        if(isUserValid){
            response.sendRedirect("lista_productos.view");
        }else{
            response.sendRedirect("login_error.jsp");
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("metodo get login");
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Metodo post login");
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
